package cn.imvc.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtils {

	public static BufferedImage createPicTwo2(String content, String loginname, String name) {
		try {
			String temName=name+"的专属身份";
			BufferedImage image = ImageIO.read(
//					new File("d:/qrcode_background.jpg")
					ImageUtils.class.getResource("/qrcode_123.jpg")
					);
			BufferedImage out = new BufferedImage(image.getWidth(), image.getHeight(), 1);
			BufferedImage image2 = QrCodeUtils.encodePRToBufferedImage(content, 200, 200);
			Graphics2D g2 = out.createGraphics();
			g2.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
			g2.drawImage(image2, (image.getWidth() - 280) / 2, 256, 270	,270, null);
//			g2.setColor(new Color(170, 23, 187));// 设置字体颜色
			Font font = new Font("黑体", Font.ITALIC, 36);
			g2.setColor(Color.red);
			g2.setFont(font);
			
			FontRenderContext context = g2.getFontRenderContext();
			// 获取字体的像素范围对象  
			Rectangle2D stringBounds = font.getStringBounds(temName, context);
			int fontWidth = (int)stringBounds.getWidth();
			Rectangle2D nameBounds = font.getStringBounds(name, context);
			int nameBoundsWidth = (int)nameBounds.getWidth();
			int  nameLeft = (image.getWidth() - fontWidth) / 2;
			g2.drawString(name, nameLeft, 660);
			Font font2 = new Font("黑体", Font.BOLD, 36);
			g2.setFont(font2);
			g2.setColor(Color.black);
			g2.drawString("的专属身份",nameLeft+nameBoundsWidth + 3, 660);
			
//			stringBounds = font.getStringBounds(name, context);
//			fontWidth = (int)stringBounds.getWidth();
//			g2.drawString(name, (image.getWidth() - fontWidth) / 2, 680);
			
			return out;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public static void main(String[] args) throws IOException {
		ImageUtils.createPicTwo2("http://105001.xinghaoshangcheng.com", "18538718834", "18538718834");
	}
}
